package com.han.nettytestdemo.connector.communication;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * socket客户端的多线程实现
 */
public class SocketClient
{
	private Socket client;
	private Context context;
	private int port;           //IP
	private String site;            //端口
	private Thread thread;
	public static Handler mHandler;
	private boolean isClient=false;
	private PrintWriter out;
	private  OutputStream out1;
	private InputStream in;
	private String str;

	/**
	 * @effect 开启线程建立连接开启客户端
	 * */
	public void openClientThread(){
		new Thread(new Runnable( )
		{
			@Override
			public void run()
			{
              while (true){
              	if(client==null||!client.isConnected()){
					Log.e("socketclient","正在连接");
				try {
					/**
					 *  connect()步骤
					 * */
					client=new Socket( site,port );

//                    client.setSoTimeout ( 5000 );//设置超时时间
					if (client!=null)
					{
						isClient=true;
						forOut();
						forIn ();
					}else {
						isClient=false;
						Log.e("socketclient","socket连接失败");
					}
					Log.i ( "socketclient","site="+site+" ,port="+port );
				}catch (Exception e) {
					e.printStackTrace ();
					Log.i ( "socketclient","6" );

				}

			}
				  try {
					  Thread.sleep(5000);//五秒判断一次
				  } catch (InterruptedException e) {
					  // TODO Auto-generated catch block
					  e.printStackTrace();
				  }
		}}}).start();


	}

	/**
	 * 调用时向类里传值
	 * */
	public void clintValue(String site, int port)
	{
		this.site=site;
		this.port=port;
		System.out.println(site+":"+port);
	}

	/**
	 * @effect 得到输出字符串
	 * */
	public void forOut()
	{
		try {
			out=new PrintWriter( client.getOutputStream () );
		}catch (IOException e){
			e.printStackTrace ();
			Log.i ( "socketclient","8" );
		}
	}
    public  void closed(){
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @steps read();
	 * @effect 得到输入字符串
	 * */
	public void forIn(){

		while (isClient) {
			try {
				in=client.getInputStream ();

				/**得到的是16进制数，需要进行解析*/
				byte[] bt = new byte[50];
				in.read ( bt );
				str=new String( bt,"UTF-8" );
			} catch (IOException e) {}
			if (str!=null) {
				Message msg = new Message( );
				msg.obj =str ;
				mHandler.sendMessage ( msg );
			}

		}
	}

	/**
	 * @steps write();
	 * @effect 发送消息
	 * */
	public void sendMsg(final byte[] str)
	{

		new Thread(new Runnable( )
		{
			@Override
			public void run()
			{
				try {
					System.out.println("Client：Connecting");
					//IP地址和端口号（对应服务端），我这的IP是本地路由器的IP地址
					Socket socket = new Socket(site, port);
					//发送给服务端的消息
					String message = "Message from Android phone";
					try {
						System.out.println("Client Sending: '" + message + "'");

						//第二个参数为True则为自动flush
						out1=socket.getOutputStream();
						out1.write(str);
  					    out1.flush();

					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						//关闭Socket
						out1.close();
						socket.close();
						System.out.println("Client:Socket closed");
					}
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}


			}
		} ).start ();

	}



}
